<template>
  <div>
    <div id="multiPie" ref="multiPie" style="height:380px;margin-bottom:16px;"></div>
  </div>
</template>

<script>
import echarts from 'echarts'
export default {
  name: 'DoughnutChart',
  data () {
    return {
      mychart: {}
    }
  },
  mounted () {
    this.initChart()
    window.addEventListener('resize', () => {
      setTimeout(() => {
        this.mychart.resize()
      }, 500)
    })
  },
  methods: {
    initChart () {
      const data = [
        {
          name: '-5~15℃',
          value: 3211
        },
        {
          name: '-15~20℃',
          value: 2111
        },
        {
          name: '-20~25℃',
          value: 1711
        },
        {
          name: '-25~30℃',
          value: 2171
        }
      ]
      const arrName = this.getArrayValue(data, 'name')
      const optionData = this.getData(data)
      this.mychart = echarts.init(this.$refs.multiPie)
      const option = {
        backgroundColor: 'rgb(0,0,0,.9)',
        title: {
          top: '16',
          left: '16',
          textStyle: {
            color: '#fff',
            fontSize: '14'
          }
        },
        legend: {
          show: true,
          bottom: '16',
          left: '16',
          data: arrName,
          itemWidth: 15,
          itemHeight: 10,
          width: 50,
          formatter: function (name) {
            return '{title|' + name + '}'
          },
          textStyle: {
            rich: {
              title: {
                fontSize: 12,
                lineHeight: 12,
                color: '#fff'
              }
            }
          }
        },
        tooltip: {
          show: true,
          trigger: 'item',
          formatter: '{a}<br>{b}:{c}({d}%)'
        },
        color: ['#FF8700', '#ffc300', '#00e473', '#009DFF'],
        grid: {
          top: '20%',
          bottom: '48%',
          left: '50%',
          containLabel: false
        },
        yAxis: [
          {
            type: 'category',
            inverse: true,
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              interval: 0,
              inside: true,
              textStyle: {
                color: '#fff',
                fontSize: 14
              },
              show: true
            },
            data: optionData.yAxis
          }
        ],
        xAxis: [
          {
            show: false
          }
        ],
        series: optionData.series
      }
      this.mychart.setOption(option)
    },
    getArrayValue (array, key) {
      key = key || 'value'
      var res = []
      if (array) {
        array.forEach(function (t) {
          res.push(t[key])
        })
      }
      return res
    },
    getData (data) {
      const arrValue = this.getArrayValue(data, 'value')
      const sumValue = this.evil(arrValue.join('+'))
      var res = {
        series: [],
        yAxis: []
      }
      for (let i = 0; i < data.length; i++) {
        res.series.push({
          name: '比例',
          type: 'pie',
          clockWise: false, // 顺时加载
          hoverAnimation: false, // 鼠标移入变大
          radius: [65 - i * 15 + '%', 57 - i * 15 + '%'],
          center: ['50%', '55%'],
          label: {
            show: false
          },
          itemStyle: {
            label: {
              show: false
            },
            labelLine: {
              show: false
            },
            borderWidth: 5
          },
          data: [
            {
              value: data[i].value,
              name: data[i].name
            },
            {
              value: sumValue - data[i].value,
              name: '',
              itemStyle: {
                color: 'rgba(0,0,0,0)',
                borderWidth: 0
              },
              tooltip: {
                show: false
              },
              hoverAnimation: false
            }
          ]
        })
        res.series.push({
          name: '',
          type: 'pie',
          silent: true,
          z: 1,
          clockWise: false, // 顺时加载
          hoverAnimation: false, // 鼠标移入变大
          radius: [65 - i * 15 + '%', 57 - i * 15 + '%'],
          center: ['50%', '55%'],
          label: {
            show: false
          },
          itemStyle: {
            label: {
              show: false
            },
            labelLine: {
              show: false
            },
            borderWidth: 5
          },
          data: [
            {
              value: 7.5,
              itemStyle: {
                color: '#E3F0FF',
                borderWidth: 0
              },
              tooltip: {
                show: false
              },
              hoverAnimation: false
            },
            {
              value: 2.5,
              name: '',
              itemStyle: {
                color: 'rgba(0,0,0,0)',
                borderWidth: 0
              },
              tooltip: {
                show: false
              },
              hoverAnimation: false
            }
          ]
        })
        res.yAxis.push(data[i].name + ' ' + ((data[i].value / sumValue) * 100).toFixed(2) + '%')
      }
      return res
    },
    evil (fn) {
      // 一个变量指向Function，防止有些前端编译工具报错
      const Fn = Function
      return new Fn('return ' + fn)()
    }
  }
}
</script>

<style lang="less" scoped></style>
